import service from './service';
import REPORT from './config/report';
import SEARCHENGINE from './config//dataManage/searchEngine';
import KEYPERSON from './config/dataManage/kerPerson';
import storageM from '../storage';

const { storageMode } = storageM;
const ServiceConf = {
  REPORT,
  SEARCHENGINE,
  KEYPERSON
};

export default {
  async post({ type, url, params = {}, storageType = 'sessionStorage', cache = false, reflush = false }) {
    let urlKey = `${ServiceConf[type]['$getway']}/${ServiceConf[type][url]}`;
    const storage = await storageMode({ storageType });    
    if (cache) {
      let storageData = storage.$get({ key: urlKey });
      if (storageData && storageData.data && !reflush) {
        return storageData;
      }
    }
    let res = await service.post(urlKey, params);
    if (res && res.code === '000') {
      if (cache) storage.$set({ key: urlKey, parameter: res });
    }
    return res;
  },

  async specPost({ type, url, params = {} }) {
    let res = await service.post(`${ServiceConf[type]['$getway']}/${ServiceConf[type][url]}`, params);
    return res.data;
  },

  async get({ type, url, params = {}, storageType = 'sessionStorage', cache = false, reflush = false }) {
    let urlKey = `${ServiceConf[type]['$getway']}/${ServiceConf[type][url]}`;
    const storage = await storageMode({ type: storageType });
   
    if (cache) {
      let storageData = storage.$get({ key: urlKey });
      if (storageData && storageData.data && !reflush) {
        return storageData;
      }
    }
    let res = await service.get(urlKey, params);
    return res;
  },

  async setHeader({ key, val }) {
    await service.setHeader({ key, val });
  }
};
